﻿using Blog.Core.EFcore;
using Blog.Core.IRepositories;
using Blog.Core.IServices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Blog.Core.Services
{
    public class BaseService<T> : IBaseService<T> where T : class, new()
    {
        private readonly IRepository<T> repository;
        private readonly IUnOfWork unOfWork;

        public BaseService(IRepository<T> repository, IUnOfWork unOfWork)
        {
            this.repository = repository;
            this.unOfWork = unOfWork;
        }

        public async Task<int> Add(T entity)
        {
            await repository.Add(entity);
            return await unOfWork.SaveChanges();
        }

        public async Task<int> Delete(int id)
        {
           await repository.Delete(id);
            return await unOfWork.SaveChanges();
        }

        public async Task<List<T>> GetAll()
        {
            return await repository.GetAll();
        }

        public async Task<T> GetById(int id)
        {
            return await repository.GetById(id);
        }

        public async Task<int> Update(T entity)
        {
            await repository.Update(entity);
            return await unOfWork.SaveChanges();
        }
    }
}
